package com.turnengine.client.local.player.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.player.bean.IPlayer;
import com.turnengine.client.local.player.command.GetPlayer;
import com.turnengine.client.local.player.command.GetPlayerActionQueueLimit;
import com.turnengine.client.local.player.command.GetPlayerById;
import com.turnengine.client.local.player.command.GetPlayerByName;
import com.turnengine.client.local.player.command.GetPlayerByUserId;
import com.turnengine.client.local.player.command.GetPlayerCount;
import com.turnengine.client.local.player.command.GetPlayerCountByFactionId;
import com.turnengine.client.local.player.command.GetPlayerLimit;
import com.turnengine.client.local.player.command.GetPlayerSignupsEnabled;
import com.turnengine.client.local.player.command.IGetPlayer;
import com.turnengine.client.local.player.command.IGetPlayerActionQueueLimit;
import com.turnengine.client.local.player.command.IGetPlayerById;
import com.turnengine.client.local.player.command.IGetPlayerByName;
import com.turnengine.client.local.player.command.IGetPlayerByUserId;
import com.turnengine.client.local.player.command.IGetPlayerCount;
import com.turnengine.client.local.player.command.IGetPlayerCountByFactionId;
import com.turnengine.client.local.player.command.IGetPlayerLimit;
import com.turnengine.client.local.player.command.IGetPlayerSignupsEnabled;
import com.turnengine.client.local.player.command.IIsPlayerSignedUp;
import com.turnengine.client.local.player.command.IRenamePlayer;
import com.turnengine.client.local.player.command.ISetPlayerActionQueueLimit;
import com.turnengine.client.local.player.command.ISetPlayerLimit;
import com.turnengine.client.local.player.command.ISetPlayerSignupPlugin;
import com.turnengine.client.local.player.command.ISetPlayerSignupsEnabled;
import com.turnengine.client.local.player.command.ISignupPlayer;
import com.turnengine.client.local.player.command.IsPlayerSignedUp;
import com.turnengine.client.local.player.command.RenamePlayer;
import com.turnengine.client.local.player.command.SetPlayerActionQueueLimit;
import com.turnengine.client.local.player.command.SetPlayerLimit;
import com.turnengine.client.local.player.command.SetPlayerSignupPlugin;
import com.turnengine.client.local.player.command.SetPlayerSignupsEnabled;
import com.turnengine.client.local.player.command.SignupPlayer;

/**
 * The Local Player Command Set.
 */
public class LocalPlayerCommandSet implements ILocalPlayerCommandSet {

	@Override
	public IPlayer signupPlayer(long loginId, int instanceId, String name, String factionName) {
		ISignupPlayer command = new SignupPlayer(loginId, instanceId, name, factionName);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IPlayer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IPlayer getPlayerById(long loginId, int instanceId, int id) {
		IGetPlayerById command = new GetPlayerById(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IPlayer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IPlayer getPlayerByUserId(long loginId, int instanceId, int id) {
		IGetPlayerByUserId command = new GetPlayerByUserId(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IPlayer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IPlayer getPlayerByName(long loginId, int instanceId, String name) {
		IGetPlayerByName command = new GetPlayerByName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IPlayer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setPlayerSignupsEnabled(long loginId, int instanceId, boolean enabled) {
		ISetPlayerSignupsEnabled command = new SetPlayerSignupsEnabled(loginId, instanceId, enabled);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean getPlayerSignupsEnabled(long loginId, int instanceId) {
		IGetPlayerSignupsEnabled command = new GetPlayerSignupsEnabled(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int setPlayerLimit(long loginId, int instanceId, int limit) {
		ISetPlayerLimit command = new SetPlayerLimit(loginId, instanceId, limit);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getPlayerLimit(long loginId, int instanceId) {
		IGetPlayerLimit command = new GetPlayerLimit(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int setPlayerActionQueueLimit(long loginId, int instanceId, int limit) {
		ISetPlayerActionQueueLimit command = new SetPlayerActionQueueLimit(loginId, instanceId, limit);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getPlayerActionQueueLimit(long loginId, int instanceId) {
		IGetPlayerActionQueueLimit command = new GetPlayerActionQueueLimit(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public String renamePlayer(long loginId, int instanceId, String from, String to) {
		IRenamePlayer command = new RenamePlayer(loginId, instanceId, from, to);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<String> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getPlayerCount(long loginId, int instanceId) {
		IGetPlayerCount command = new GetPlayerCount(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getPlayerCountByFactionId(long loginId, int instanceId, int id) {
		IGetPlayerCountByFactionId command = new GetPlayerCountByFactionId(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean isPlayerSignedUp(long loginId, int instanceId) {
		IIsPlayerSignedUp command = new IsPlayerSignedUp(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setPlayerSignupPlugin(long loginId, int instanceId, String plugin) {
		ISetPlayerSignupPlugin command = new SetPlayerSignupPlugin(loginId, instanceId, plugin);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IPlayer getPlayer(long loginId, int instanceId) {
		IGetPlayer command = new GetPlayer(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IPlayer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
